package com.example.applicationdataactivity;

import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import android.Manifest;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.content.IntentSender;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.media.ExifInterface;
import android.media.MediaMetadataRetriever;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.ParcelFileDescriptor;
import android.provider.MediaStore;
import android.util.Size;
import android.widget.Toast;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Set;
import java.util.concurrent.TimeUnit;

///访问共享存储空间中的媒体文件
public class MediaStoreActivity extends AppCompatActivity {
    private static final int EDIT_REQUEST_CODE = 1;

    @RequiresApi(api = Build.VERSION_CODES.Q)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_media_store);
        getVersion(this);
        ///手动获取权限
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE}, 1);
        } else {
            queryMediaCollection(this);
        }
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_MEDIA_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.ACCESS_MEDIA_LOCATION}, 1);
        } else {
            queryPhotoCollection(this);
        }
        checkOutOtherVolume(this);
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE}, 1);
        } else {
            ///添加媒体
            Uri uri = addMedia();
            ///更新媒体
            updateProject(uri);
            ///管理媒体组
//            try {
//                manageMediaGroupCreateWriteRequest(uri);
//            } catch (IntentSender.SendIntentException e) {
//                e.printStackTrace();
//            }
        }
    }

    ///媒体库
    private void getMediaStore() {
        String[] projection = new String[]{};
        String seleciton = "";
        String[] selectionArgs = new String[]{};
        String sortOrder = "";
        @SuppressLint("Recycle") Cursor cursor = getApplicationContext().getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
//                MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
//                MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
//                MediaStore.Downloads.EXTERNAL_CONTENT_URI,
//                MediaStore.Files,
                projection, seleciton, selectionArgs, sortOrder);
        while (cursor.moveToNext()) {

        }
    }

    ///获取媒体库的版本
    private void getVersion(Context context) {
        String v = MediaStore.getVersion(context);
    }

    ///查询媒体集合（视频）
    private void queryMediaCollection(Context context) {
        List<Video> videoList = new ArrayList<>();
        Uri collection;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
            collection = MediaStore.Video.Media.getContentUri(MediaStore.VOLUME_EXTERNAL);
        } else {
            collection = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
        }
        String[] projection = new String[]{MediaStore.Video.Media._ID, MediaStore.Video.Media.DISPLAY_NAME, MediaStore.Video.Media.DURATION, MediaStore.Video.Media.SIZE, MediaStore.Video.Media.DATA,};
        String selection = MediaStore.Video.Media.DURATION + ">= ?";
        String[] selectionArgs = new String[]{String.valueOf(TimeUnit.MILLISECONDS.convert(1, TimeUnit.MINUTES))};
        String sortOrder = MediaStore.Video.Media.DISPLAY_NAME + " ASC";
        try (Cursor cursor = context.getContentResolver().query(collection, projection, selection, selectionArgs, sortOrder)) {
            int idColumn = cursor.getColumnIndexOrThrow(MediaStore.Video.Media._ID);
            int nameColumn = cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DISPLAY_NAME);
            int durationColumn = cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DURATION);
            int sizeColumn = cursor.getColumnIndexOrThrow(MediaStore.Video.Media.SIZE);
            int dataColumn = cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA);
            while (cursor.moveToNext()) {
                long id = cursor.getLong(idColumn);
                String name = cursor.getString(nameColumn);
                int duration = cursor.getInt(durationColumn);
                int size = cursor.getInt(sizeColumn);
                String path = cursor.getString(dataColumn);
                System.out.println(path);
                Uri contentUri = ContentUris.withAppendedId(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, id);
                ///加载文件缩略图
                Bitmap bitmap = loadFileThumbnail(contentUri);
                ///打开媒体文件
                openMediaFile(contentUri);
                ///获取媒体的位置信息
                getVideoInfo(contentUri, context);
                ///原生代码更新
                remoteCodeUpdate(contentUri);
                videoList.add(new Video(contentUri, name, duration, size));
            }
            Toast.makeText(context, videoList.size() + "个", Toast.LENGTH_LONG).show();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    ///查询媒体集合（图片）
    private void queryPhotoCollection(Context context) {
        Uri collection;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
            collection = MediaStore.Images.Media.getContentUri(MediaStore.VOLUME_EXTERNAL);
        } else {
            collection = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
        }
        String[] projection = new String[]{MediaStore.Images.Media._ID, MediaStore.Images.Media.DISPLAY_NAME, MediaStore.Images.Media.SIZE, MediaStore.Images.Media.DATA,};
        String sortOrder = MediaStore.Images.Media.DISPLAY_NAME + " ASC";
        try (Cursor cursor = context.getContentResolver().query(collection, projection, null, null, sortOrder)) {
            int idColumn = cursor.getColumnIndexOrThrow(MediaStore.Images.Media._ID);
            int nameColumn = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DISPLAY_NAME);
            int sizeColumn = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.SIZE);
            int data = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            while (cursor.moveToNext()) {
                long id = cursor.getLong(idColumn);
                String name = cursor.getString(nameColumn);
                int size = cursor.getInt(sizeColumn);
                Uri contentUri = ContentUris.withAppendedId(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, id);
                String path = cursor.getString(data);
                ///获取媒体的拍摄位置
                getPhotoInfo(contentUri);
            }
        }
    }

    ///加载文件缩略图
    private Bitmap loadFileThumbnail(Uri contentUri) throws IOException {
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.Q) {
            return getApplicationContext().getContentResolver().loadThumbnail(contentUri, new Size(640, 480), null);
        }
        return null;
    }

    ///打开媒体文件
    private void openMediaFile(Uri contentUri) throws IOException {
        ContentResolver resolver = getApplicationContext().getContentResolver();
        //rw 读和写; rwt 覆盖已有的文件
        String readOnlyMode = "r";
        //文件描述符
        try (ParcelFileDescriptor parcelFileDescriptor = resolver.openFileDescriptor(contentUri, readOnlyMode)) {

        } catch (IOException e) {
            e.printStackTrace();
        }
        //文件流
        try (InputStream stream = resolver.openInputStream(contentUri)) {

        }
    }

    ///查看其他分卷
    private void checkOutOtherVolume(Context context) {
        Set<String> volumeNames = null;
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.Q) {
            volumeNames = MediaStore.getExternalVolumeNames(context);
            String firstVolumeName = volumeNames.iterator().next();
        }
    }

    ///获取媒体的拍摄位置(图片)
    private void getPhotoInfo(Uri photoUri) {
        float[] latLong = new float[2];
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
            photoUri = MediaStore.setRequireOriginal(photoUri);
            InputStream stream = null;
            try {
                stream = getApplicationContext().getContentResolver().openInputStream(photoUri);
                if (stream != null) {
                    ExifInterface exifInterface = new ExifInterface(stream);
                    boolean returnedLatLong = exifInterface.getLatLong(latLong);
                    if (returnedLatLong) {
                        float la = latLong[0];
                        float lo = latLong[1];
                    } else {
                        latLong = new float[2];
                    }
                    stream.close();
                } else {
                    latLong = new float[2];
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

        } else {
            latLong = new float[2];
        }
    }

    //获取媒体的位置信息
    private void getVideoInfo(Uri videoUri, Context context) {
        MediaMetadataRetriever retriever = new MediaMetadataRetriever();
        try {
            retriever.setDataSource(context, videoUri);
        } catch (RuntimeException e) {
            e.printStackTrace();
        }
        String locationMetadata = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_LOCATION);
    }

    //添加媒体到媒体库中
    private Uri addMedia() {
        ContentResolver resolver = getApplicationContext().getContentResolver();
        Uri videoCollection;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
//            videoCollection = MediaStore.Video.Media.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY);
            videoCollection = MediaStore.Video.Media.getContentUri(MediaStore.VOLUME_EXTERNAL);
        } else {
            videoCollection = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
        }
        ContentValues newSongDetails = new ContentValues();
        Random random = new Random();
        double number = random.nextInt(1000);
        newSongDetails.put(MediaStore.Video.Media.DISPLAY_NAME, "My Video" + number + ".mp4");
        Uri uri = resolver.insert(videoCollection, newSongDetails);
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            try (Cursor cursor = getApplicationContext().getContentResolver().query(uri, new String[]{MediaStore.Video.Media.DATA}, null, null)) {
                int data = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
                while (cursor.moveToNext()) {
                    System.out.println(cursor.getString(data));
                }
            }
        }
        return uri;
    }

    ///耗时操作写入媒体
    @RequiresApi(api = Build.VERSION_CODES.Q)
    private void addMediaWithinLongTime() {
        ContentResolver resolver = getApplicationContext().getContentResolver();
        Uri audioCollection;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
            audioCollection = MediaStore.Audio.Media.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY);
        } else {
            audioCollection = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
        }

        ContentValues songDetails = new ContentValues();
        songDetails.put(MediaStore.Audio.Media.DISPLAY_NAME, "My Workout Playlist.mp3");
        songDetails.put(MediaStore.Audio.Media.IS_PENDING, 1);

        Uri songContentUri = resolver.insert(audioCollection, songDetails);

        try (ParcelFileDescriptor pfd = resolver.openFileDescriptor(songContentUri, "w", null)) {
        } catch (IOException e) {
            e.printStackTrace();
        }
        songDetails.clear();
        songDetails.put(MediaStore.Audio.Media.IS_PENDING, 0);
        resolver.update(songContentUri, songDetails, null, null);
    }

    ///更新项目(自己app创建的可以修改)
    private void updateProject(Uri uri) {
        ContentResolver resolver = getApplicationContext().getContentResolver();
        ContentValues updatedSongDetails = new ContentValues();
        updatedSongDetails.put(MediaStore.Video.Media.DISPLAY_NAME, "My Favorite video.mp4");
        try (ParcelFileDescriptor pfd = resolver.openFileDescriptor(uri, "w", null)) {
            String path = uri.getPath();
            int numSongsUpdated = resolver.update(uri, updatedSongDetails, null, null);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    ///原生代码更新
    private void remoteCodeUpdate(Uri contentUri) throws FileNotFoundException {
        ContentResolver resolver = getApplicationContext().getContentResolver();
        String fileOpenMode = "r";
        @SuppressLint("Recycle") ParcelFileDescriptor parcelFd = resolver.openFileDescriptor(contentUri, fileOpenMode);
        if (parcelFd != null) {
            int fd = parcelFd.detachFd();
            // Pass the integer value "fd" into your native code. Remember to call
            // close(2) on the file descriptor when you're done using it.
        }
    }

    ///删除媒体
    private void deleteMedia(Uri uri) {
        ContentResolver contentResolver = getApplicationContext().getContentResolver();
        String selection = "";
        String[] selectionArgs = {""};
        int numImagesRemoved = contentResolver.delete(uri, selection, selectionArgs);
    }

    ///检查媒体文件的更新
    private void checkMediaUpdate(Context context, String volumeName) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
            long number = MediaStore.getGeneration(context, volumeName);
        }
    }

    ///管理媒体文件组 createWriteRequest()
    private void manageMediaGroupCreateWriteRequest(Uri uri) throws IntentSender.SendIntentException {

        ContentResolver contentResolver = getApplicationContext().getContentResolver();
        List<Uri> urisToModify = new ArrayList<>();
        urisToModify.add(uri);
        PendingIntent editPendingIntent = null;
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.R) {
            editPendingIntent = MediaStore.createWriteRequest(contentResolver, urisToModify);
            startIntentSenderForResult(editPendingIntent.getIntentSender(), EDIT_REQUEST_CODE, null, 0, 0, 0);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == EDIT_REQUEST_CODE) {
            if (resultCode == Activity.RESULT_OK) {
                /* Edit request granted; proceed. */
            } else {
                /* Edit request not granted; explain to the user. */
            }
        }
    }
}

///视频类
class Video {
    private final Uri uri;
    private final String name;
    private final int duration;
    private final int size;

    public Video(Uri uri, String name, int duration, int size) {
        this.uri = uri;
        this.name = name;
        this.duration = duration;
        this.size = size;
    }
}
